package cn.exrick.xboot.modules.research.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;

import cn.exrick.xboot.modules.research.entity.SubjectManagement;
import cn.exrick.xboot.modules.research.service.ISubjectManagementService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lty
 */
@Slf4j
@RestController
@Api(tags = "科研课题管理管理接口")
@RequestMapping("/xboot/subjectManagement")
@Transactional
public class SubjectManagementController {

    @Autowired
    private ISubjectManagementService iSubjectManagementService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<SubjectManagement> get(@PathVariable String id) {

        SubjectManagement subjectManagement = iSubjectManagementService.getById(id);
        return new ResultUtil<SubjectManagement>().setData(subjectManagement);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<SubjectManagement>> getAll() {

        List<SubjectManagement> list = iSubjectManagementService.list();
        return new ResultUtil<List<SubjectManagement>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<SubjectManagement>> getByPage(@ModelAttribute SubjectManagement subjectManagement,@ModelAttribute PageVo page) {
        QueryWrapper<SubjectManagement> qw =new QueryWrapper<>();

        if (StrUtil.isNotBlank(subjectManagement.getSubjectId())){
             qw.like("subject_Id",subjectManagement.getSubjectId());
        }

        if (StrUtil.isNotBlank(subjectManagement.getSubjectName())){
            qw.like("subject_Name",subjectManagement.getSubjectName());
        }

        if (StrUtil.isNotBlank(subjectManagement.getLeader())){
            qw.like("leader",subjectManagement.getLeader());
        }

        if (StrUtil.isNotBlank(subjectManagement.getParticipant())){
            qw.like("participant",subjectManagement.getParticipant());
        }

        if (StrUtil.isNotBlank(subjectManagement.getUnit())){
            qw.like("unit",subjectManagement.getUnit());
        }

        if (StrUtil.isNotBlank(subjectManagement.getTime())){
            qw.like("time",subjectManagement.getTime());
        }

        IPage<SubjectManagement> data = iSubjectManagementService.page(PageUtil.initMpPage(page),qw);
        return new ResultUtil<IPage<SubjectManagement>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<SubjectManagement> saveOrUpdate(SubjectManagement subjectManagement) {

        if (iSubjectManagementService.saveOrUpdate(subjectManagement)) {
            return new ResultUtil<SubjectManagement>().setData(subjectManagement);
        }
        return new ResultUtil<SubjectManagement>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@RequestParam String[] ids) {

        for (String id : ids) {
            iSubjectManagementService.removeById(id);
        }
        return ResultUtil.success("批量通过id删除数据成功");
    }
}
